(function($) {
$(document).ready(function() {
	
	
	var disableBlur = false;
	
    $.fn.shake = function (options) {
        // defaults
        var settings = {
            'shakes': simplecontactform_shake_count,
            'distance': simplecontactform_shake_distanse,
            'duration':simplecontactform_shake_duration
        };
        // merge options
        if (options) {
            $.extend(settings, options);
        }
        // make it so
        var pos;
        return this.each(function () {
            $this = $(this);
            // position if necessary
            pos = $this.css('position');
            if (!pos || pos === 'static') {
                $this.css('position', 'relative');
            }
            // shake it
            for (var x = 1; x <= settings.shakes; x++) {
                $this.animate({ left: settings.distance * -1 }, (settings.duration / settings.shakes) / 4)
                    .animate({ left: settings.distance }, (settings.duration / settings.shakes) / 2)
                    .animate({ left: 0 }, (settings.duration / settings.shakes) / 4);
            }
        });
    };
			
	
			function simplecontactform_validate(fild,value,required) {
				value = jQuery.trim( value );
				switch(fild) {
				case 'email':
					if(!required && value == '')
						return true;
					else {
						var i = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
						if(i)
							return true;
						else
							return false;
					}
				break;
				case 'phone':
					if(!required && value == '')
						return true;
					else {
						var i = /^[0-9\-\(\)\_\: ]+$/i.test(value);
						if(i)
							return true;
						else
							return false;
					}
					break;
				case 'name':
					if(!required && value == '')
						return true;
					else {
						if(value.length > 0)
							return true;
						else
							return false;
					}
					break;
				case 'message':
					if(!required && value == '')
						return true;
					else {
						if(value.length > 0)
							return true;
						else
							return false;
					}
				break;
				default:
					return true;
				}
			}
			
			function simplecontactform_make_validation($this,validate_filds) {
				var $email = $this.parents('.simplecontactform_wrapper').find("#simplecontactform_email");
				var $name = $this.parents('.simplecontactform_wrapper').find("#simplecontactform_name");
				var $phone = $this.parents('.simplecontactform_wrapper').find("#simplecontactform_phone");
				var $message = $this.parents('.simplecontactform_wrapper').find("#simplecontactform_message");
				
				//start validation
				var simplecontactformValid = true;
				
				//validate name
				if(jQuery.inArray("name", validate_filds) != -1) {
					var simplecontactformNameRequired = $name.hasClass('simplecontactform_required') ? true : false;
					if(!simplecontactform_validate('name',$name.val(),simplecontactformNameRequired)) {
						$name.parent('div').addClass('simplecontactform_error');
						if(validate_filds.length > 1)
							$name.focus();
						simplecontactformValid = false;
						$name.shake();
					}
					else
						$name.parent('div').removeClass('simplecontactform_error');
				}
				
				//validate email
				if(jQuery.inArray("email", validate_filds) != -1) {
					var simplecontactformEmailRequired = $email.hasClass('simplecontactform_required') ? true : false;
					if(!simplecontactform_validate('email',$email.val(),simplecontactformEmailRequired)) {
						$email.parent('div').addClass('simplecontactform_error');
						if(simplecontactformValid && validate_filds.length > 1)
							$email.focus();
						simplecontactformValid = false;
						$email.shake();
					}
					else
						$email.parent('div').removeClass('simplecontactform_error');
				}
				
				//validate phone
				if(jQuery.inArray("phone", validate_filds) != -1) {
					var simplecontactformPhoneRequired = $phone.hasClass('simplecontactform_required') ? true : false;
					if(!simplecontactform_validate('phone',$phone.val(),simplecontactformPhoneRequired)) {
						$phone.parent('div').addClass('simplecontactform_error');
						if(simplecontactformValid && validate_filds.length > 1)
							$phone.focus();
						simplecontactformValid = false;
						$phone.shake();
					}
					else
						$phone.parent('div').removeClass('simplecontactform_error');
				}
				
				//validate message
				if(jQuery.inArray("message", validate_filds) != -1) {
					var simplecontactformMessageRequired = $message.hasClass('simplecontactform_required') ? true : false;
					if(!simplecontactform_validate('message',$message.val(),simplecontactformMessageRequired)) {
						$message.parent('div').addClass('simplecontactform_error');
						if(simplecontactformValid && validate_filds.length > 1)
							$message.focus();
						simplecontactformValid = false;
						$message.shake();
					}
					else
						$message.parent('div').removeClass('simplecontactform_error');
				}
				
				return simplecontactformValid;
			}
			
			$('.simplecontactform_send').mousedown(function() {
				disableBlur = true;
			})
			$('.simplecontactform_send').click(function() {
				disableBlur = false;
				
				//animate loading
				var loading_element = $(this).parents('.simplecontactform_wrapper').children('.simplecontactform_loading_wrapper');
				var pre_element = $(this).parents('.simplecontactform_wrapper').find('.simplecontactform_pre_text');
				var send_button = $(this).parents('.simplecontactform_wrapper').find('.simplecontactform_send');
				var send_new_button = $(this).parents('.simplecontactform_wrapper').find('.simplecontactform_send_new');

				if(simplecontactform_make_validation($(this),["name","email","phone","message"])) {
					animate_loading_start(loading_element);
					var simplecontactform_data = $(this).parents('form').serialize();
					//send request
					$.ajax
					({
						url: simplecontactform_path + 'sendmail.php',
						type: "post",
						data: simplecontactform_data,
						dataType: "json",
						success: function(data)
						{
							if(data[0].invalid == 'invalid_token') {
								alert('Invalid Token');
								return;
							}
							//generates console info
							var l = data[0].info.length;
							for(var i  = 0;i <= l; i++) {
								if(data[0].info[i] != undefined)
									console.log(data[0].info[i])
							};
							
							//animate back
							animate_loading_end(loading_element);
							
							//check redirect
							if(simplecontactform_redirect_enable == 1) {
								if(simplecontactform_redirect != '') {
									setTimeout(function() {
										window.location.href(simplecontactform_redirect);
									},simplecontactform_redirect_delay);
								}
							};
							
							//show thank you text
							if(simplecontactform_thank_you_text != 'blank')
								pre_element.html(simplecontactform_thank_you_text);
							
							//replace buttons 
							if(simplecontactform_redirect_enable == 0) {
								send_new_button.show();
								send_button.hide();
							}
						},
						error: function()
						{
						}
					});
				}
			});
			
			$('.simplecontactform_send_new').click(function() {
				var loading_element = $(this).parents('.simplecontactform_wrapper').children('.simplecontactform_loading_wrapper');
				var pre_element = $(this).parents('.simplecontactform_wrapper').find('.simplecontactform_pre_text');
				var simplecontactform_field_box = $(this).parents('.simplecontactform_wrapper').find('.simplecontactform_field_box');
				var send_button = $(this).parents('.simplecontactform_wrapper').find('.simplecontactform_send');
				var send_new_button = $(this).parents('.simplecontactform_wrapper').find('.simplecontactform_send_new');
				var simplecontactform_input_element  = $(this).parents('.simplecontactform_wrapper').find('.simplecontactform_input_element ');
				
				animate_loading_start(loading_element);
				$.ajax
				({
					url: simplecontactform_path + 'sendmail.php',
					type: "get",
					data: 'get_token=1',
					success: function(data)
					{
						//animate back
						animate_loading_end(loading_element);
						
						//set token
						$('.simplecontactform_token').attr("name",data);
						
						//replace buttons
						send_new_button.hide();
						send_button.show();
						
						//clear inputs
						simplecontactform_input_element.val('');
						simplecontactform_input_element.html('');
						
						//clear errors
						simplecontactform_field_box.removeClass('simplecontactform_error');
						
						//set pre text
						pre_element.html(simplecontactform_pre_text);
						
					},
					error: function()
					{
					}
				});
			});
			
			function animate_loading_start($elem) {
				$elem
				.css({opacity:0,display:'block'})
				.stop()
				.animate({
					opacity: 0.45
				},400);
			};
			function animate_loading_end($elem) {
				$elem
				.stop()
				.animate({
					opacity: 0
				},400,function(){
					$(this).hide();
				});
			};
			
			//blur validation
			$("#simplecontactform_name").blur(function() {
				if(!disableBlur)
					simplecontactform_make_validation($(this),["name"]);
			});
			$("#simplecontactform_email").blur(function() {
				if(!disableBlur)
					simplecontactform_make_validation($(this),["email"]);
			});
			$("#simplecontactform_phone").blur(function() {
				if(!disableBlur)
					simplecontactform_make_validation($(this),["phone"]);
			});
			$("#simplecontactform_message").blur(function() {
				if(!disableBlur)
					simplecontactform_make_validation($(this),["message"]);
			});
			
			
			
	
})

})(jQuery);